<?php 
namespace PsiFramework\Classes\Util\Parameters\DAO;

import("Classes.Util.Parameters.Object.AttributeParameters");
import("Classes.Util.Manager.ApplicationManager");

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * The ParametersDAO class is the generic class to retrieve parameters
 *
 * @package Util.Parameters.DAO
 */
class ParametersDAO
{

    protected static /*ParametersDAO*/ $_singleton = null ;

    public static function getDocumentRoot(/*string*/ $file)
    {
        $document = new \DomDocument();
        $document->load($file);
        return $document->documentElement;
    }

    public static function getXMLStringRoot(/*string*/ $xml)
    {
        if (isNullOrEmptyString($xml)) {
            return null ;
        }

        $document = new \DomDocument();
        $document->loadXML($xml);
        return $document->documentElement;
    }

    public function read(\DOMElement $racine)
    {

        $parameters = array();
        $parametersElement = $racine->getElementsByTagName("parameter");

        foreach ($parametersElement as $childNode) {
            $name = $childNode->getAttribute("name");
            $value = $childNode->getAttribute("value");
            $parameters[$name] = $value ;
        }
        return $parameters ;
    }

    public static function getInstance()
    {

        if (is_null(self::$_singleton)) {
            self::$_singleton = new ParametersDAO();
        }
        return self::$_singleton ;
    }

}
